package com.songhuan.web.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songhuan.web.dto.*;
import com.songhuan.web.dto.query.*;
import com.songhuan.web.entity.*;
import com.songhuan.web.mapper.*;
import com.songhuan.web.service.*;
import com.songhuan.web.tools.dto.*;
import com.songhuan.web.dto.AppUserDto;
import com.songhuan.web.dto.LineUploadDto;
import com.songhuan.web.dto.query.LineUploadPagedInput;
import com.songhuan.web.entity.AppUser;
import com.songhuan.web.entity.LineUpload;
import com.songhuan.web.mapper.AppUserMapper;
import com.songhuan.web.mapper.LineUploadMapper;
import com.songhuan.web.service.LineUploadService;
import com.songhuan.web.tools.BaseContext;
import com.songhuan.web.tools.Extension;
import com.songhuan.web.tools.dto.CurrentUserDto;
import com.songhuan.web.tools.dto.PagedResult;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import lombok.SneakyThrows;
import com.songhuan.web.tools.*;
/**
 * 线路实时上报功能实现类
 */
@Service
public class LineUploadServiceImpl extends ServiceImpl<LineUploadMapper, LineUpload> implements LineUploadService {

	 /**
     * 操作数据库AppUser表mapper对象
     */
    @Autowired
    private AppUserMapper _AppUserMapper;
    /**
     * 操作数据库的LineUpload表mapper对象
     */
    @Autowired
    private LineUploadMapper _LineUploadMpper;

 			
			

    /**
     * 线路实时上报分页查询
     */
    @SneakyThrows
    @Override
    public PagedResult<LineUploadDto> List(LineUploadPagedInput input) {

      
        //声明一个支持线路实时上报查询的(拉姆达)表达式
        LambdaQueryWrapper<LineUpload> queryWrapper = Wrappers.<LineUpload>lambdaQuery()
                .eq(input.getId()!=null&&input.getId()!=0,LineUpload::getId,input.getId())
                .eq(input.getCreatorId()!=null,LineUpload::getCreatorId,input.getCreatorId());
   //如果前端搜索传入查询条件则拼接查询条件
         if(input.getLongitudeStartRange()!=null)
          {
            queryWrapper=queryWrapper.gt(LineUpload::getLongitude,input.getLongitudeStartRange());
          }
      		 if(input.getLongitudeEndRange()!=null)
          {
            queryWrapper=queryWrapper.lt(LineUpload::getLongitude,input.getLongitudeEndRange());
          }

        if(Extension.isNotNullOrEmpty(input.getLatitude())) {
            queryWrapper=queryWrapper.eq(LineUpload::getLatitude,input.getLatitude());
       	 }
        if(input.getUserId()!=null) {
            queryWrapper=queryWrapper.eq(LineUpload::getUserId,input.getUserId());
       	 }
      
        //按创建时间从大到小排序 最新的显示在最前面
        queryWrapper=queryWrapper.orderByDesc(LineUpload::getCreationTime);
        //构建一个分页查询的model
        Page<LineUpload> page = new Page<>(input.getPage(), input.getLimit());
          
   
         //从数据库进行分页查询获取线路实时上报数据
        IPage<LineUpload> pageRecords= _LineUploadMpper.selectPage(page, queryWrapper);

        //获取所有满足条件的数据行数
        Long totalCount= _LineUploadMpper.selectCount(queryWrapper);

        //把LineUpload实体转换成LineUpload传输模型
        List<LineUploadDto> items= Extension.copyBeanList(pageRecords.getRecords(),LineUploadDto.class);
        for (LineUploadDto item : items) {
          
          //查询出关联的创建用户信息
            AppUserDto CreatorAppUserDTO=new AppUserDto();
            AppUser CreatorAppUserEntity= _AppUserMapper.selectOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getId,item.getCreatorId()));
           if(CreatorAppUserEntity!=null) {
                BeanUtils.copyProperties(CreatorAppUserDTO, CreatorAppUserEntity);
                item.setCreatorAppUserDto(CreatorAppUserDTO);
            }  
         
           
          	            
           //查询出关联的AppUser表信息
           AppUserDto UserDTO = new AppUserDto();        
            AppUser  UserEntity= _AppUserMapper.selectOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getId,item.getUserId()));
           if(UserEntity!=null) {  
          BeanUtils.copyProperties(UserDTO,UserEntity);
            item.setUserDto(UserDTO); 
           }
          
        }
      
        //返回一个分页结构给前端
        return PagedResult.GetInstance(items,totalCount);

    }
  
    /**
     * 单个线路实时上报查询
     */
    @SneakyThrows
    @Override
    public LineUploadDto Get(LineUploadPagedInput input) {
       if(input.getId()==null)
        {
            return new LineUploadDto();
        }
        PagedResult<LineUploadDto>  pagedResult =List(input);
        return pagedResult.getTotalCount()>0?pagedResult.getItems().stream().findFirst().get():new LineUploadDto();
    }

    /**
     *线路实时上报创建或者修改
     */
    @SneakyThrows
    @Override
    public LineUploadDto CreateOrEdit(LineUploadDto input) {

        //声明一个线路实时上报实体
        LineUpload LineUpload = new LineUpload();

        //把前端传入的input参数拷贝到线路实时上报实体
        BeanUtils.copyProperties(LineUpload,input);

        //调用数据库的增加或者修改方法
        saveOrUpdate(LineUpload);

        //定义一个返回给前端的线路实时上报传输模型
        LineUploadDto LineUploadDto=new LineUploadDto();

        //同理把操作的线路实时上报实体拷贝给线路实时上报传输模型
        BeanUtils.copyProperties(LineUploadDto,LineUpload);

        //把传输模型返回给前端
        return LineUploadDto;
    }
    /**
     * 同步当前数据
     */
    @SneakyThrows
    @Override
    public void SyncLocation(LineUploadDto input) {
        CurrentUserDto currentUserDto = BaseContext.getCurrentUserDto();
        if (currentUserDto != null && currentUserDto.getUserId() != null && currentUserDto.getUserId() > 0) {
            input.setUserId(currentUserDto.getUserId());
            input.setCreatorId(currentUserDto.getUserId());

            input.setLatitude(input.getLatitude());
            input.setLongitude(input.getLongitude());
            CreateOrEdit(input);

        }
    }


}
